<?php defined('SYSPATH') OR die('No direct access allowed.');

class Member_Controller extends Master_Admin_Controller 
{
		
	public function index()
	{
		$this->page(1);
	}
	
	public function page($pagenum=1)
    {
		$view = new View('admin/member');

		$this->set_title('ผู้ดูแลระบบ - สมาชิก');
    	
        $profiles = new Profiles_Model;

        // Instantiate Pagination, passing it the total number of product rows.
        $paging = new Pagination(array
		(
			'base_url'    => 'admin/member/ajax_page/',
			'uri_segment' => 4,
			'total_items' => $profiles->count_members(),
		));

        // Render the page links
        $view->pagination = $paging->render();

        // Display X items per page, from offset = page number
        $view->members = $profiles->browse($paging->items_per_page, $paging->sql_offset);

    	$this->template->content = $view;
		$this->template->render(TRUE);
        
    }

    public function ajax_page($pagenum=1, $close=false)
    {
    	if(request::is_ajax())
	    {
	    	$this->auto_render=false;
	    	$ajax = new ISS_Ajax();
	    	
	    	$view = new View('admin/member');
	    	$profiles = new Profiles_Model;

	        // Instantiate Pagination, passing it the total number of product rows.
	        $paging = new Pagination(array
			(
				'base_url'    => 'admin/member/ajax_page/',
				'uri_segment' => 4,
				'total_items' => $profiles->count_members(),
			));
	
	        // Render the page links
	        $view->pagination = $paging->render();
	
	        // Display X items per page, from offset = page number
	        $view->members = $profiles->browse($paging->items_per_page, $paging->sql_offset);
	    	$selector = "mainCont";
		    $content = $view;
			$ajax->addViewUpdate($selector, $content);
			
			if ($close) {
		    	$tag = "close";
			    $script = "closePopUp();";
				$ajax->addExecJScript($tag, $script);
			}
			
			echo json_encode($ajax->getHtmlAjax());
	    }
	    else {
	    	$this->page($pagenum);
	    }
    }
    
    public function del($id) {
    	if(request::is_ajax())
	    {
	    	$this->auto_render=false;
	    	$ajax = new ISS_Ajax();
	    	
	    	$profiles = new Profiles_Model;
	    	if ($profiles->delete($id)) {
	    		$this->ajax_page(1);
	    	}
	    	else {
		    	$tag = "alert";
			    $script = "alert('". Kohana::lang('profile_error.save_result.delete_failed') ."')";
				$ajax->addExecJScript($tag, $script);
				echo json_encode($ajax->getHtmlAjax());
	    	}
	    }
	    else {
	    	$this->page(1);
	    }
    	
    }
    
    public function paid($id) {
    	if(request::is_ajax())
	    {
	    	$this->auto_render=false;
	    	$ajax = new ISS_Ajax();
	    	
	    	$profiles = new Profiles_Model;
	    	if ($profiles->paid($id)) {
	    		$this->ajax_page(1, true);	    		
	    	}
	    	else {
		    	$tag = "alert";
			    $script = "alert('". Kohana::lang('profile_error.save_result.paid_failed') ."')";
				$ajax->addExecJScript($tag, $script);
				echo json_encode($ajax->getHtmlAjax());
	    	}
	    }
	    else {
	    	$this->page(1);
	    }
    }
    
	public function request_paid($id) {
		$view = new View("admin/member_evident");
        $view->title = "ยืนยันการชำระเงิน";

        $profiles = new Profiles_Model;
        $evident = $profiles->get_evident($id);
        
        $form = array (
        			'id' => '',
					'username'    => '',
					'member_type'    => '',
        			'reference_id'   => '',
					'evident_picture' => '',
				);
				
		foreach ($evident as $row) {
			$form['id'] = $row['id'];
			$form['username'] = $row['firstname'] . ' ' . $row['lastname'];
			$form['member_type'] = $row['member_type'];
			$form['reference_id'] = $row['reference_id'];
			$form['evident_picture'] = $row['evident_picture'];
		}
        $view->form = $form;
        $view->render(TRUE);		
	}

}
